dnl Check for required program and exit if non-existant.
dnl LK_AC_CHECK_REQ_PROG( VAR, PROG)
AC_DEFUN([LK_AC_CHECK_REQ_PROG],[
  AC_ARG_VAR($1, [Full path to required program $2])
  AC_PATH_PROG($1,$2,[])
  if test "${$1}x" = "x" ; then
    AC_MSG_ERROR( ["Required program $2 not found."])
  fi
])

dnl Check for program and set variable if not available
dnl LK_AC_CHECK_OPT_PROG( VAR, PROG)
AC_DEFUN([LK_AC_CHECK_OPT_PROG],[
  AC_ARG_VAR($1, [Full path to optional program $2])
  AC_PATH_PROG($1,$2,[])
  if test "${$1}x" = "x" ; then
    AC_MSG_WARN( ["Program $2 not found. Some functionality is disabled."])
    [HAVE_$1_FLAG=0]
  else
    [HAVE_$1_FLAG=1]
  fi
  AC_SUBST([HAVE_$1_FLAG])
  AM_CONDITIONAL([HAVE_$1], test "${$1}x" != "x")
])


dnl Check for latex and supporting programs to be used with latex.mk
dnl LK_AC_CHECK_LATEX_DOCU
AC_DEFUN([LK_AC_CHECK_LATEX_DOCU],[
  LK_AC_CHECK_OPT_PROG(LATEX,latex)
  LK_AC_CHECK_OPT_PROG(DVIPS,dvips)
  LK_AC_CHECK_OPT_PROG(LATEX2HTML,latex2html)
  LK_AC_CHECK_OPT_PROG(PDFLATEX,pdflatex)
])


dnl Add a variable containing the path to the installed perl modules. Keep
dnl this syncronized with perl.mk.
dnl AC_PREP_PERL_MODULES
AC_DEFUN([LK_AC_PREP_PERL_MODULES],[
  oldprefix="$prefix"
  oldexec_prefix="$exec_prefix"
  test "x$prefix" = xNONE && prefix="$ac_default_prefix"
  test "x$exec_prefix" = xNONE && exec_prefix="${prefix}"
  PATH_TO_PERL_MODULES=`eval eval echo "${datadir}/${PACKAGE}-${VERSION}/src"`
  PATH_TO_PERL_SCRIPTS=`eval eval echo "${bindir}"`
  prefix="$oldprefix"
  exec_prefix="$oldexec_prefix"
  AC_MSG_NOTICE([Perl modules go beneath '${PATH_TO_PERL_MODULES}'.  You can't change that using make.])
  AC_MSG_NOTICE([Perl scripts go beneath '${PATH_TO_PERL_SCRIPTS}'.  You can't change that using make.])
  AC_SUBST(PATH_TO_PERL_MODULES)
  AC_SUBST(PATH_TO_PERL_SCRIPTS)
])

dnl Try to run a piece of perl code. The code comes from AC_TRY_RUN.
dnl LK_AC_TRY_RUN_PERL( perl-code, [code-if-ok], [code-if-failed])
AC_DEFUN([LK_AC_TRY_RUN_PERL],[
  rm -f conftest.pl
  cat >conftest.pl <<_ACEOF
$1
  exit(0);
_ACEOF
  
  AS_IF([AC_TRY_COMMAND([${PERL} conftest.pl])],[$2],[$3])
  rm -f conftest.pl
])

dnl Check whether a specific perl module exists in the general perl path.
dnl LK_AC_CHECK_PERL_MODULE( module-name, code-if-exists, code-if-not-exists)
AC_DEFUN([LK_AC_CHECK_PERL_MODULE],[
  LK_AC_TRY_RUN_PERL([use $1;],[$2],[$3])
])

dnl Require a specific perl module and fail if it does not exists.
dnl LK_AC_CHECK_REQ_PERL_MODULE( module-name)
AC_DEFUN([LK_AC_CHECK_REQ_PERL_MODULE],[
   AC_MSG_CHECKING([whether perl module $1 is usable])
   LK_AC_CHECK_PERL_MODULE([$1],[
     AC_MSG_RESULT([yes])
   ],[
     AC_MSG_RESULT([no])
     AC_MSG_NOTICE([You may get that module from CPAN. Consult 'man CPAN' to learn how.])
     AC_MSG_ERROR([Required perl module $1 not found.])
   ])
])

dnl Require a library and fail if it does not exists.
AC_DEFUN([LK_AC_CHECK_REQ_LIB],[
  AC_CHECK_LIB($1,$2,,[
    AC_MSG_ERROR([Required library lib$1 not found.])
  ],$3)
])

dnl Require a library and fail if it does not exists. Hint about the download location
AC_DEFUN([LK_AC_CHECK_REQ_LIBH],[
  AC_CHECK_LIB($1,$2,,[
    AC_MSG_ERROR([Required library lib$1 not found. Get it from $4])
  ],$3)
])


dnl Set the flags according to the selected version.
AC_DEFUN([LK_AC_SET_BUILD_MODE],[
  AC_ARG_ENABLE(build,
    AC_HELP_STRING([--enable-build={debug|dprof|rprof|fprof|ftest|test|fast|release}],
      [Use build mode:
        * debug:        Debug symbols,   no optimisation
        * dprof:        Debug Profiling, no optimisation
        * rprof:        Release Profiling, full optimisation
        * fprof:        Release Profiling, fast optimisation
        * ftest:        Debug symbols,   fast optimisation
        * test:         Debug symbols,   full optimisation
        * fast:         No debug,        fast optimisation
        * release:      No debug,        full optimisation]),
    ac_cv_buildmode=$enableval, ac_cv_buildmode=release)
  AC_CACHE_CHECK([for build mode], ac_cv_buildmode, ac_cv_buildmode=release)
  case $ac_cv_buildmode in
   debug)
     CFLAGS="-g -Wall"
     ;;
   dprof)
     CFLAGS="-g -pg -Wall"
     ;;
   rprof)
     CFLAGS="-g -pg -O3 -DNDEBUG"
     ;;
   fprof)
     CFLAGS="-g -pg -O2 -DNDEBUG"
     ;;
   test)
     CFLAGS="-g -O3 -Wall -DNDEBUG"
     ;;
   ftest)
     CFLAGS="-g -O2 -Wall -DNDEBUG"
     ;;
   fast)
     CFLAGS="-O2 -Wall -DNDEBUG"
     ;;
   release)
     CFLAGS="-O3 -Wall -DNDEBUG"
     ;;
   *) 
    AC_MSG_ERROR([Unknown build mode $ac_cv_buildmode])
    ;;
  esac
  CXXFLAGS=$CFLAGS
])

                    
dnl Check for perl executable
AC_DEFUN([LK_AC_CHECK_PERL],[
  LK_AC_CHECK_REQ_PROG(PERL,perl)
])

dnl Helper function to get info from perl
AC_DEFUN([_LK_GETPERL_CONFIGVAR],[
  AC_MSG_CHECKING([for perl config variable $2])
  eval `\$PERL -V:$2`;
  $1="$$2";
  AC_MSG_RESULT([$$1])
  AC_SUBST($1)
])

dnl Setup all the little things for perl xs compilation
AC_DEFUN([LK_AC_CHECK_PERLXS],[
  AC_REQUIRE([LK_AC_CHECK_PERL])
  _LK_GETPERL_CONFIGVAR( PERLXS_LD, ld)
  _LK_GETPERL_CONFIGVAR( PERLXS_DLEXT, dlext)
  _LK_GETPERL_CONFIGVAR( PERLXS_CCDLFLAGS, ccdlflags)
  _LK_GETPERL_CONFIGVAR( PERLXS_CCCDLFLAGS, cccdlflags)
  _LK_GETPERL_CONFIGVAR( PERLXS_LDDLFLAGS, lddlflags)
  _LK_GETPERL_CONFIGVAR( PERLXS_ARCHLIBEXT, archlibexp)
  _LK_GETPERL_CONFIGVAR( PERLXS_INSTALLSTYLE, installstyle)
  _LK_GETPERL_CONFIGVAR( PERLXS_VERSION, version)
  _LK_GETPERL_CONFIGVAR( PERLXS_ARCHNAME, archname)
  oldprefix="$prefix"
  oldexec_prefix="$exec_prefix"
  test "x$prefix" = xNONE && prefix="$ac_default_prefix"
  test "x$exec_prefix" = xNONE && exec_prefix="${prefix}"
  PATH_TO_PERL_XS="$prefix/$PERLXS_INSTALLSTYLE/site_perl/$PERLXS_VERSION/$PERLXS_ARCHNAME";
  prefix="$oldprefix"
  exec_prefix="$oldexec_prefix"
  AC_SUBST( PATH_TO_PERL_XS)
])

dnl Set the install pathes for libraries
AC_DEFUN([LK_AC_SET_LIB_INSTALL_PATHES],[
  oldprefix="$prefix"
  oldexec_prefix="$exec_prefix"
  test "x$prefix" = xNONE && prefix="$ac_default_prefix"
  test "x$exec_prefix" = xNONE && exec_prefix="${prefix}"

  INSTALL_PATH_INCLUDE=`eval echo "${includedir}"`

  prefix="$oldprefix"
  exec_prefix="$oldexec_prefix"

  AC_MSG_NOTICE([Include files are assumed to go beneath '${INSTALL_PATH_INCLUDE}'.  Don't change that using make or other packages won't find this library.])
  AC_SUBST(INSTALL_PATH_INCLUDE)
])

dnl Call the config program of a certain package.
dnl _LK_AC_CALL_CONFIG(pkg,outvar,param)
AC_DEFUN([_LK_AC_CALL_CONFIG],[
  $2=`$1-config --$3`
])

dnl Check if a config program exists and yields a sufficient package version
dnl LK_AC_CHECK_REQ_PKG(package,version,url)
AC_DEFUN([LK_AC_CHECK_REQ_PKG],[
  AC_PATH_PROG($1_PATH,$1-config,[])
  AC_MSG_CHECKING([for package $1])
  if test "${$1_PATH}x" = "x" ; then
    AC_MSG_ERROR( ["Program $1-config not found. Obtain package $1 from $3"])
  fi
  _LK_AC_CALL_CONFIG($1,$1_VERSION,version)
  if [[[ "${$1_VERSION}" < '$2' ]]] ; then
    AC_MSG_ERROR( ["Package $1 is too old. Obtain at least version $2 from $3"])
  fi
  AC_MSG_RESULT([version ${$1_VERSION} from ${$1_PATH}])
])

dnl Check if a config program exists and yields a sufficient package version
dnl LK_AC_CHECK_OPT_PKG(package,version,url)
AC_DEFUN([LK_AC_CHECK_OPT_PKG],[
  AC_PATH_PROG($1_PATH,$1-config,[])
  AC_MSG_CHECKING([for package $1])
  have_package="yes"
  if test "${$1_PATH}x" = "x" ; then
    have_package="no"
  else
    _LK_AC_CALL_CONFIG($1,$1_VERSION,version)
    if [[[ "${$1_VERSION}" < '$2' ]]] ; then
      have_package="no"
    fi
  fi
  if test "$have_package" = "yes" ; then
    AC_MSG_RESULT([version ${$1_VERSION} from ${$1_PATH}])
    [HAVE_$1_FLAG=1]
  else
    AC_MSG_RESULT([not available. Obtain $1 >version $2 from $3. Some functionality will be disabled.])
    [HAVE_$1_FLAG=0]
  fi
  AC_SUBST([HAVE_$1_FLAG])
  AM_CONDITIONAL([HAVE_$1], test "$have_package" = "yes")
])

dnl Obtain the flags for c++ compilation.
dnl LK_AC_CHECK_REQ_OPT_CXXINFO(package)
AC_DEFUN([LK_AC_CHECK_OPT_PKG_CXXINFO],[
  if test "$[HAVE_$1_FLAG]" = "1" ; then
    LK_AC_CHECK_REQ_PKG_CXXINFO($1,$2)
  fi
])

dnl Obtain the path to the perl modules from the config program of a package.
dnl LK_AC_CHECK_REQ_PKG_PERLPATH(var,package)
AC_DEFUN([LK_AC_CHECK_REQ_PKG_PERLPATH],[
  AC_MSG_CHECKING([for perl path of package $2])
  _LK_AC_CALL_CONFIG($2,$1,perlpath)
  AC_MSG_RESULT($$1)
  AC_SUBST($1)
])

dnl Obtain the flags for c++ compilation.
dnl LK_AC_CHECK_REQ_PKG_CXXINFO(package)
AC_DEFUN([LK_AC_CHECK_REQ_PKG_CXXINFO],[
  AC_MSG_CHECKING([for c++ info of package $2])
  _LK_AC_CALL_CONFIG($1,$1_CXXFLAGS,cxxflags)
  _LK_AC_CALL_CONFIG($1,$1_LDFLAGS,ldflags)
  _LK_AC_CALL_CONFIG($1,$1_LIBS,libs)
  _LK_AC_CALL_CONFIG($1,$1_INCLUDEDIR,includedir)
  CXXFLAGS="$CXXFLAGS $$1_CXXFLAGS"
  LDFLAGS="$LDFLAGS $$1_LDFLAGS"
  LIBS="$LIBS $$1_LIBS"
  if test "$1_INCLUDEDIR" != "" ; then
    CXXFLAGS="$CXXFLAGS -I$$1_INCLUDEDIR"
  fi
  AC_MSG_RESULT([CXXFLAGS: $$1_CXXFLAGS, LDFLAGS: $$1_LDFLAGS, LIBS: $$1_LIBS])
])

dnl Check if POSIX regexes are available
dnl LK_AC_CHECK_REQ_REGEX
AC_DEFUN([LK_AC_CHECK_REQ_REGEX],[
  AC_CHECK_FUNC( regfree, [], [
    AC_MSG_ERROR( ["POSIX regular expressions not available."])
  ])
])


dnl AC_CHECK_LIB_WITH_INCLUDE(INCLUDES, LIBRARY, FUNCTION,
dnl              [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND],
dnl              [OTHER-LIBRARIES])
AC_DEFUN([AC_CHECK_LIB_WITH_INCLUDE],
[m4_ifval([$4], , [AH_CHECK_LIB([$2])])dnl
AS_LITERAL_IF([$2],
              [AS_VAR_PUSHDEF([ac_Lib], [ac_cv_lib_$2_$3])],
              [AS_VAR_PUSHDEF([ac_Lib], [ac_cv_lib_$2''_$3])])dnl
AC_CACHE_CHECK([for $3 in -l$2], ac_Lib,
[ac_check_lib_save_LIBS=$LIBS
LIBS="-l$2 $6 $LIBS"
AC_TRY_LINK([$1], [extern int $3(); $3();],
                 [AS_VAR_SET(ac_Lib, yes)],
                 [AS_VAR_SET(ac_Lib, no)])
LIBS=$ac_check_lib_save_LIBS])
AS_IF([test AS_VAR_GET(ac_Lib) = yes],
      [m4_default([$4], [AC_DEFINE_UNQUOTED(AS_TR_CPP(HAVE_LIB$2))
  LIBS="-l$2 $LIBS"
])],
      [$5])dnl
AS_VAR_POPDEF([ac_Lib])dnl
])

dnl Check for lapack
dnl LK_AC_CHECK_REQ_LAPACK
AC_DEFUN([LK_AC_CHECK_REQ_LAPACK],[
  for appdir in . $APP_DIRS; do
    echo "void MAIN__() { }" > $appdir/dummyMain.c
  done
  AC_CHECK_LIB_WITH_INCLUDE( [#include "dummyMain.c"], lapack, dgetrf_, [
    LIBS="$LIBS -lblas -lf2c"
    AC_LIBOBJ(dummyMain)
  ], [
    AC_MSG_ERROR( [lapack not available. Get it from http://www.netlib.org])
  ], [-lblas -lf2c])
])

dnl Perl module install prefix handling: Find in src dir.
dnl LK_AC_PERL_DEFAULT_PREFIX
AC_DEFUN([LK_AC_PERL_DEFAULT_PREFIX],[
  LK_PERL_REMOVE_PREFIX='src';
  AC_SUBST(LK_PERL_REMOVE_PREFIX)
])

dnl Perl module install prefix handling: Remove a prefix.
dnl LK_AC_PERL_REMOVE_PREFIX( prefix)
AC_DEFUN([LK_AC_PERL_REMOVE_PREFIX],[
  LK_PERL_REMOVE_PREFIX='$1';
  AC_SUBST(LK_PERL_REMOVE_PREFIX)
])


dnl ========================== ruby ===========================
dnl Check for ruby executable
AC_DEFUN([LK_AC_CHECK_RUBY],[
  LK_AC_CHECK_REQ_PROG(RUBY,ruby)
])

dnl Try to run a piece of ruby code. The code comes from AC_TRY_RUN.
dnl LK_AC_TRY_RUN_RUBY( ruby-code, [code-if-ok], [code-if-failed])
AC_DEFUN([LK_AC_TRY_RUN_RUBY],[
  rm -f conftest.rb
  cat >conftest.rb <<_ACEOF
$1
  exit(0)
_ACEOF
  
  AS_IF([AC_TRY_COMMAND([${RUBY} conftest.rb])],[$2],[$3])
  rm -f conftest.rb
])


dnl Helper function to get info from ruby
AC_DEFUN([_LK_GETRUBY_CONFIGVAR],[
  AC_MSG_CHECKING([for ruby config variable $2])
  $1=`\$RUBY -e 'require "rbconfig"; print Config::CONFIG[["$2"]]'`
  AC_MSG_RESULT([$$1])
  AC_SUBST($1)
])

dnl Setup all the little things for linking against ruby
AC_DEFUN([LK_AC_CHECK_RUBYLIB],[
  AC_REQUIRE([LK_AC_CHECK_RUBY])
  _LK_GETRUBY_CONFIGVAR( LIBRUBY_archdir, archdir)
  _LK_GETRUBY_CONFIGVAR( LIBRUBY_hdrdir, rubyhdrdir)
  _LK_GETRUBY_CONFIGVAR( LIBRUBY_SO_NAME, RUBY_SO_NAME)
  AC_CHECK_LIB($LIBRUBY_SO_NAME, main, , ,)
])

